﻿using System;
using System.Data;
using UniMana.Data;
using UniMana.Utils.Data;
using System.Collections.Generic;
namespace UniMana.Win.Training
{
    public partial class FrmSchedule_SubChooseClass
    {
        //lấy danh sách lớp
        private void GetClass()
        {
            int iCouserKey = -1;
            int iDepartmentKey = -1;
            try
            {
                //iCouserKey = lueCatalogue_Course.GetIntValue();
                //iDepartmentKey = lueCatalogue_Department.GetIntValue();
            }
            catch
            {
            }
            if (iCouserKey != -1)
            {
                if (iDepartmentKey != -1)
                {
                    m_dtaClass =
                        m_DBMain.m_DCReadOnly.Training_Classes.GetClassByProgramKey(
                            m_DBMain.m_DCReadOnly.Training_Subject_Classes,
                            m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                            m_DBMain.m_DCReadOnly.Training_Student_Classes,
                            m_ilSubjectKey,
                            -1,
                            iCouserKey,
                            iDepartmentKey).ToDataTable();
                    gctTraining_Class.DataSource = m_dtaClass;
                    grvTraining_Class_Choose.ExpandAllGroups();
                    grvTraining_Class.ExpandAllGroups();


                }
                else
                {
                    m_dtaClass =
                        m_DBMain.m_DCReadOnly.Training_Classes.GetClassByProgramKey(
                           m_DBMain.m_DCReadOnly.Training_Subject_Classes,
                           m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                           m_DBMain.m_DCReadOnly.Training_Student_Classes,
                           m_ilSubjectKey,
                           -1,
                           iCouserKey).ToDataTable();
                    gctTraining_Class.DataSource = m_dtaClass;
                    grvTraining_Class.ExpandAllGroups();
                    grvTraining_Class_Choose.ExpandAllGroups();
                }
            }
            else
            {
                if (iDepartmentKey != -1)
                {
                    gctTraining_Class.DataSource =
                        m_DBMain.m_DCReadOnly.Training_Classes.GetClassByProgramKey(
                            m_DBMain.m_DCReadOnly.Training_Subject_Classes,
                            m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                            m_DBMain.m_DCReadOnly.Training_Student_Classes,
                            m_ilSubjectKey,
                            -1,
                            iDepartmentKey);
                    grvTraining_Class.ExpandAllGroups();
                    grvTraining_Class_Choose.ExpandAllGroups();
                }
                else
                {
                    gctTraining_Class.DataSource =
                       m_DBMain.m_DCReadOnly.Training_Classes.GetClassByProgramKey(
                           m_DBMain.m_DCReadOnly.Training_Subject_Classes,
                           m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details,
                           m_DBMain.m_DCReadOnly.Training_Student_Classes,
                           m_ilSubjectKey);
                    grvTraining_Class.ExpandAllGroups();
                    grvTraining_Class_Choose.ExpandAllGroups();
                }
            }
            grvTraining_Class.ExpandAllGroups();
        }
        //chọn sinh viên
        private void SelectSt(int k)
        {
            int[] row = grvTraining_Student_Class.GetSelectedRows();
            if (row.Length > 0)
            {
                for (int i = 0; i < row.Length; i++)
                {
                    Guid personelKey =
                        grvTraining_Student_Class.GetRowCellValue(
                            row[i],
                            gclTraining_StudentKey_Left).ToGuid();
                    string strID =
                        grvTraining_Student_Class.GetRowCellValue(
                            row[i],
                            gclID_Left).ToString();
                    string strLastName =
                        grvTraining_Student_Class.GetRowCellValue(
                            row[i],
                            gclLastName_Left).ToString();
                    string strSureName = grvTraining_Student_Class.GetRowCellValue(
                            row[i],
                            gclSureName_Left).ToString();
                    string strBirthDay =
                        grvTraining_Student_Class.GetRowCellValue(
                            row[i],
                            gclBirthday_Left).ToString();
                    int iTraining_TrainingProgramKey =
                        grvTraining_Student_Class.GetRowCellValue(
                            row[i],
                            gclTraining_TrainingProgramKey_Left).ToInt();
                    m_l_dta_Student[k].Rows.Add(
                        personelKey,
                        strID,
                        strLastName,
                        strSureName,
                        strBirthDay,
                        iTraining_TrainingProgramKey);
                    m_dtaChoose.Rows[k]["Count"] = Convert.ToInt16( m_dtaChoose.Rows[k]["Count"].ToString()) + 1;
                }
                grvTraining_Student_Class.DeleteSelectedRows();
            }
            gctTraining_Class_Choose.DataSource = m_dtaChoose;
            grvTraining_Class.ExpandAllGroups();
            grvTraining_Class_Choose.ExpandAllGroups();
        }
        //bỏ chọn sinh viên
        private void RemoveSt(int k)
        {
            int[] row = grvTraining_Teacher_Student.GetSelectedRows();
            if (row.Length > 0)
            {
                for (int i = 0; i < row.Length; i++)
                {
                    int handle = grvTraining_Teacher_Student.GetRowHandle(row[i]);
                    m_l_dta_Student[k].Rows.RemoveAt(handle);
                    m_dtaChoose.Rows[k]["Count"] = Convert.ToInt16(m_dtaChoose.Rows[k]["Count"].ToString()) - 1;
                }
            }
        }
        //Khởi tạo dữ liệu của datatable danh sách lớp đã chọn
        private void SetDataClassChoose(DataTable dt = null)
        {
            m_dtaChoose.Rows.Clear();
            m_dtaChoose.Columns.Clear();
            m_dtaChoose.Columns.Add("Training_ClassKey");
            m_dtaChoose.Columns.Add("ID");
            m_dtaChoose.Columns.Add("Name");
            m_dtaChoose.Columns.Add("Catalogue_DepartmentKey");
            m_dtaChoose.Columns.Add("Catalogue_CourseKey");
            m_dtaChoose.Columns.Add("Catalogue_LevelKey");
            m_dtaChoose.Columns.Add("Catalogue_MajorKey");
            m_dtaChoose.Columns.Add("Catalogue_TrainingTypeKey");
            m_dtaChoose.Columns.Add("Status");
            m_dtaChoose.Columns.Add("Count");
            m_dtaChoose.Columns.Add("i");

            ///khởi tạo khi load dữ liệu cũ
            if (dt != null)
            {
                int count = dt.Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    m_dtaChoose.Rows.Add(
                        dt.Rows[i]["Training_ClassKey"],
                        dt.Rows[i]["ID"],
                        dt.Rows[i]["Name"],
                        dt.Rows[i]["Catalogue_DepartmentKey"],
                        dt.Rows[i]["Catalogue_CourseKey"],
                        dt.Rows[i]["Catalogue_LevelKey"],
                        dt.Rows[i]["Catalogue_MajorKey"],
                        dt.Rows[i]["Catalogue_TrainingTypeKey"],
                        dt.Rows[i]["Status"],
                        dt.Rows[i]["Count"], i);
                }
            }
        }
        //Lưu thông tin về lớp đã chọn khi đóng form vào cache
        private void SaveClassToCache()
        {
            Cache.m_dtaClass_Student.Clear();
            for (int k = 0; k < 3; k++)
            {
                DataTable dt = new DataTable();
                IntalDataStudent(ref dt);
                Cache.m_dtaClass_Student.Add(dt);
                
                for (int i = 0; i < m_dtaClass.Rows.Count; i++)
                {
                    Cache.m_dtaClass_Student[k].Rows.Add(m_l_dta_Student[k].Rows[i]["Training_StudentKey"],
                                                  m_l_dta_Student[k].Rows[i]["ID"],
                                                  m_l_dta_Student[k].Rows[i]["LastName"],
                                                  m_l_dta_Student[k].Rows[i]["SureName"],
                                                  m_l_dta_Student[k].Rows[i]["Birthday"],
                                                  m_l_dta_Student[k].Rows[i]["Training_TrainingProgramKey"]);
                }
            }
        }
        //Khởi tạo dữ liệu list datatable sinh viên
        private void IntalDataStudent(ref DataTable dt)
        {
            dt.Rows.Clear();
            dt.Columns.Clear();
            dt.Columns.Add("Training_StudentKey");
            dt.Columns.Add("ID");
            dt.Columns.Add("LastName");
            dt.Columns.Add("SureName");
            dt.Columns.Add("Birthday");
            dt.Columns.Add("Training_TrainingProgramKey");
        }
        //Lưu Vị trí các dòng đã chọn của datatable m_dtaChose so với Gridview-grvTraining_Class_Choose
        private void ResetIndexm_dtaChoose(DataTable m_dtaChoose)
        {
            for (int i = 0; i < m_dtaChoose.Rows.Count; i++)
            {
                m_dtaChoose.Rows[i]["i"] = i;
            }
        }
        //Chép dữ liệu của dt_from datatable tới dt_to
        private void CoppyData_Add(DataTable dt_from, ref DataTable dt_to,bool coppy=true)
        {
            if(coppy)
            {
                dt_to.Rows.Clear();
                dt_to.Columns.Clear();
                for (int i = 0; i < dt_from.Columns.Count; i++)
                {
                    dt_to.Columns.Add(dt_from.Columns[i].ColumnName);
                }
            }
            for (int j = 0; j < dt_from.Rows.Count; j++)
            {
                dt_to.Rows.Add();
                for (int i = 0; i < dt_from.Columns.Count; i++)
                {
                    dt_to.Rows[j][i] = dt_from.Rows[j][i];
                }
            }
        }
        //Chọn Lớp
        private void ChooseClass(int i)
        {
            ResetIndexm_dtaChoose(m_dtaChoose);
            m_dtaChoose.Rows.Add(
                                grvTraining_Class.GetRowCellValue(i, "Training_ClassKey"),
                                grvTraining_Class.GetRowCellValue(i, "ID"),
                                grvTraining_Class.GetRowCellValue(i, "Name"),
                                grvTraining_Class.GetRowCellValue(i, "Catalogue_DepartmentKey"),
                                grvTraining_Class.GetRowCellValue(i, "Catalogue_CourseKey"),
                                grvTraining_Class.GetRowCellValue(i, "Catalogue_LevelKey"),
                                grvTraining_Class.GetRowCellValue(i, "Catalogue_MajorKey"),
                                grvTraining_Class.GetRowCellValue(i, "Catalogue_TrainingTypeKey"),
                                grvTraining_Class.GetRowCellValue(i, "Status"),
                                0,
                                m_dtaChoose.Rows.Count);
            DataTable dt = new DataTable();
            IntalDataStudent(ref dt);
            m_l_dta_Student.Add(dt);
        }
        //Bỏ Chọn Lớp
        private void RemoveClass(int k, DataTable draf)
        {
            draf.Rows.RemoveAt(k);
        }
        //Kiểm tra lớp trùng
        private bool CheckClass(string key)
        {
            for (int i = 0; i < m_dtaChoose.Rows.Count; i++)
            {
                string keychoosen = m_dtaChoose.Rows[i]["Training_ClassKey"].ToString();
                if (key == keychoosen)
                {
                    return false;
                }
            }
            return true;
        }
        //Lấy Danh Sách Sinh Viên Theo Lớp Đã Chọn
        private List<int> GetClassAsKey(int key,DataTable m_dtaChoose)
        {
            List <int > numclass = new List<int>();
            for (int i = 0; i < m_dtaChoose.Rows.Count; i++)
            {
                int newkey = Convert.ToInt16( m_dtaChoose.Rows[i][ "Training_ClassKey"].ToString());
                if (key == newkey)
                {
                    numclass.Add(Convert.ToInt16(m_dtaChoose.Rows[i]["i"].ToString()));
                }
            }
            return numclass;
        }
        //Thết lập dữ liệu về danh sách sinh viên
        private void Setm_dtaLeft()
        {
            //reset dữ liệu
            m_dtaLeft.Rows.Clear();
            //lấy ds lớp trùng nhau
            List<int> listclass = new List<int>();
            int k = Convert.ToInt16(
                grvTraining_Class_Choose.GetFocusedRowCellValue("Training_ClassKey").ToString());
            int m_i = Convert.ToInt16(
                grvTraining_Class_Choose.GetFocusedRowCellValue("i").ToString());
            listclass = GetClassAsKey(k, m_dtaChoose);
            //lấy dssv đã đăng ký
            //lớp đang chọn
            gctTraining_Teacher_Student.DataSource = m_l_dta_Student[m_i];
            //các lớp khác
            DataTable draf = new DataTable();
            IntalDataStudent(ref draf);
            for (int i = 0; i < listclass.Count; i++)
            {
                CoppyData_Add(m_l_dta_Student[listclass[i]], ref draf, false);
            }
            //lấy dssv của lớp
            m_dtaLeft = m_DBMain.m_DCReadOnly.Training_Classes.GetListStudentByClassKey(
                m_DBMain.m_DCReadOnly.Training_Student_Classes,
                m_DBMain.m_DCReadOnly.Training_Students,
                Convert.ToInt16(
                    grvTraining_Class_Choose.GetFocusedRowCellValue(
                        "Training_ClassKey").ToString())
                ).ToDataTable();
            //bỏ dvsv đã đăng ký ra khỏi dssv của lớp
            for (int i = 0; i < m_dtaLeft.Rows.Count; i++)
            {
                for (int j = 0; j < draf.Rows.Count; j++)
                {
                    if (m_dtaLeft.Rows[i]["Training_StudentKey"].ToString() ==
                        draf.Rows[j]["Training_StudentKey"].ToString())
                    {
                        m_dtaLeft.Rows.Remove(m_dtaLeft.Rows[i]);
                        i--;
                        break;
                    }
                }
            }
            gctTraining_Student_Class.DataSource = m_dtaLeft;
        }
    }
}
